<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Date math support in index names | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'date-math-index-names.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/date-math-index-names.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/date-math-index-names.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/date-math-index-names.html" rel="nofollow" target="_blank">../en/date-math-index-names.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="rest-apis.html">REST APIs</a></span>
»
<span class="breadcrumb-link"><a href="api-conventions.html">API conventions</a></span>
»
<span class="breadcrumb-node">Date math support in index names</span>
</div>
<div class="navheader">
<span class="prev">
<a href="multi-index.html">« Multiple indices</a>
</span>
<span class="next">
<a href="cron-expressions.html">Cron expressions »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="date-math-index-names"></a>Date math support in index names<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/api-conventions.asciidoc">edit</a>
</h2>
</div></div></div>
<p>Date math index name resolution enables you to search a range of time-series indices, rather
than searching all of your time-series indices and filtering the results or maintaining aliases.
Limiting the number of indices that are searched reduces the load on the cluster and improves
execution performance. For example, if you are searching for errors in your
daily logs, you can use a date math name template to restrict the search to the past
two days.</p>
<p>Almost all APIs that have an <code class="literal">index</code> parameter support date math in the <code class="literal">index</code> parameter
value.</p>
<p>A date math index name takes the following form:</p>
<div class="pre_wrapper lang-txt">
<pre class="programlisting prettyprint lang-txt">&lt;static_name{date_math_expr{date_format|time_zone}}&gt;</pre>
</div>
<p>Where:</p>
<div class="informaltable">
<table border="0" cellpadding="4px">
<colgroup>
<col>
<col>
</colgroup>
<tbody valign="top">
<tr>
<td valign="top">
<p>
<code class="literal">static_name</code>
</p>
</td>
<td valign="top">
<p>
is the static text part of the name
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">date_math_expr</code>
</p>
</td>
<td valign="top">
<p>
is a dynamic date math expression that computes the date dynamically
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">date_format</code>
</p>
</td>
<td valign="top">
<p>
is the optional format in which the computed date should be rendered. Defaults to <code class="literal">yyyy.MM.dd</code>. Format should be compatible with java-time <a href="https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html" class="ulink" target="_top">https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html</a>
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">time_zone</code>
</p>
</td>
<td valign="top">
<p>
is the optional time zone. Defaults to <code class="literal">utc</code>.
</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>Pay attention to the usage of small vs capital letters used in the <code class="literal">date_format</code>. For example:
<code class="literal">mm</code> denotes minute of hour, while <code class="literal">MM</code> denotes month of year. Similarly <code class="literal">hh</code> denotes the hour in the
<code class="literal">1-12</code> range in combination with <code class="literal">AM/PM</code>, while <code class="literal">HH</code> denotes the hour in the <code class="literal">0-23</code> 24-hour range.</p>
</div>
</div>
<p>Date math expressions are resolved locale-independent. Consequently, it is not possible to use any other
calendars than the Gregorian calendar.</p>
<p>You must enclose date math index name expressions within angle brackets, and
all special characters should be URI encoded. For example:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console"># GET /&lt;logstash-{now/d}&gt;/_search
GET /%3Clogstash-%7Bnow%2Fd%7D%3E/_search
{
  "query" : {
    "match": {
      "test": "data"
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1331.console"></div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<h3>Percent encoding of date math characters</h3>
<p>The special characters used for date rounding must be URI encoded as follows:</p>
<div class="informaltable">
<table border="0" cellpadding="4px">
<colgroup>
<col>
<col>
</colgroup>
<tbody valign="top">
<tr>
<td valign="top">
<p>
<code class="literal">&lt;</code>
</p>
</td>
<td valign="top">
<p>
<code class="literal">%3C</code>
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">&gt;</code>
</p>
</td>
<td valign="top">
<p>
<code class="literal">%3E</code>
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">/</code>
</p>
</td>
<td valign="top">
<p>
<code class="literal">%2F</code>
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">{</code>
</p>
</td>
<td valign="top">
<p>
<code class="literal">%7B</code>
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">}</code>
</p>
</td>
<td valign="top">
<p>
<code class="literal">%7D</code>
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">|</code>
</p>
</td>
<td valign="top">
<p>
<code class="literal">%7C</code>
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">+</code>
</p>
</td>
<td valign="top">
<p>
<code class="literal">%2B</code>
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">:</code>
</p>
</td>
<td valign="top">
<p>
<code class="literal">%3A</code>
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">,</code>
</p>
</td>
<td valign="top">
<p>
<code class="literal">%2C</code>
</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>The following example shows different forms of date math index names and the final index names
they resolve to given the current time is 22nd March 2024 noon utc.</p>
<div class="informaltable">
<table border="1" cellpadding="4px">
<colgroup>
<col class="col_1">
<col class="col_2">
</colgroup>
<thead>
<tr>
<th align="left" valign="top">Expression</th>
<th align="left" valign="top">Resolves to</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p><code class="literal">&lt;logstash-{now/d}&gt;</code></p></td>
<td align="left" valign="top"><p><code class="literal">logstash-2024.03.22</code></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">&lt;logstash-{now/M}&gt;</code></p></td>
<td align="left" valign="top"><p><code class="literal">logstash-2024.03.01</code></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">&lt;logstash-{now/M{yyyy.MM}}&gt;</code></p></td>
<td align="left" valign="top"><p><code class="literal">logstash-2024.03</code></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">&lt;logstash-{now/M-1M{yyyy.MM}}&gt;</code></p></td>
<td align="left" valign="top"><p><code class="literal">logstash-2024.02</code></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">&lt;logstash-{now/d{yyyy.MM.dd|+12:00}}&gt;</code></p></td>
<td align="left" valign="top"><p><code class="literal">logstash-2024.03.23</code></p></td>
</tr>
</tbody>
</table>
</div>
<p>To use the characters <code class="literal">{</code> and <code class="literal">}</code> in the static part of an index name template, escape them
with a backslash <code class="literal">\</code>, for example:</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
<code class="literal">&lt;elastic\{ON\}-{now/M}&gt;</code> resolves to <code class="literal">elastic{ON}-2024.03.01</code>
</li>
</ul>
</div>
<p>The following example shows a search request that searches the Logstash indices for the past
three days, assuming the indices use the default Logstash index name format,
<code class="literal">logstash-yyyy.MM.dd</code>.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console"># GET /&lt;logstash-{now/d-2d}&gt;,&lt;logstash-{now/d-1d}&gt;,&lt;logstash-{now/d}&gt;/_search
GET /%3Clogstash-%7Bnow%2Fd-2d%7D%3E%2C%3Clogstash-%7Bnow%2Fd-1d%7D%3E%2C%3Clogstash-%7Bnow%2Fd%7D%3E/_search
{
  "query" : {
    "match": {
      "test": "data"
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1332.console"></div>
</div>
<div class="navfooter">
<span class="prev">
<a href="multi-index.html">« Multiple indices</a>
</span>
<span class="next">
<a href="cron-expressions.html">Cron expressions »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>